package com.dhcc.sds.busi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.cache.config.CdcConfigCache;
import com.dhcc.cdc.sys.entity.ConfigurationEntity;
import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.cdc.sys.service.IConfigurationService;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.busi.dao.SdsFillByDoctorMapper;
import com.dhcc.sds.busi.dao.SdsQcincaseMapper;
import com.dhcc.sds.busi.dto.SdsFillByDoctorDto;
import com.dhcc.sds.busi.entity.SdsQcincaseEntity;
import com.dhcc.sds.busi.service.ISdsFillByDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 病种入组病例表数据服务层接口
 *
 * @author sunyingjie
 * @ClassName: SdsFillByDoctorServiceImpl
 * @Description: TODO
 * @date 2021-03-22 10:09:15
 */
@Service("sdsFillByDoctorService")
public class SdsFillByDoctorServiceImpl extends BaseServiceImpl<SdsQcincaseMapper, SdsQcincaseEntity> implements ISdsFillByDoctorService {


    @Autowired
    private SdsFillByDoctorMapper sdsFillByDoctorMapper;

    @Autowired
    private IDictionaryService dictionaryService;

    @Autowired
    private ISdsCommonService sdsCommonService;

    @Autowired
    private IConfigurationService configurationService;

    @Override
    public List<SdsQcincaseEntity> page(Page<SdsQcincaseEntity> page, Map<String, Object> map) {
        List<SdsQcincaseEntity> result = new ArrayList<>();

        Object entityId = map.get("entityId");
        if (CommonUtil.isEmpty(entityId)) {
            return result;
        }
        Object searchType = map.get("searchType");
        Object startDate = map.get("startDate");
        Object endDate = map.get("endDate");
        Object status = map.get("status");
        Long aLong = Long.valueOf(entityId.toString());
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        map.put("userLinkDepts", deptIds);

        // 单病种医生是否可以直接上报配置
        String formCheckType = CdcConfigCache.me().getValueByKey("SDS", "SDFormCheckType");
        String sqlStr = "";
        if (CommonUtil.isNotEmpty(status)) {
            switch (status.toString()) {
                case "notReported":
                    if (CommonUtil.equals(formCheckType,"Y")) {
                        sqlStr = "and (state = 'I' or state = 'Save' or state = 'Back' or state = 'Submit' or state = 'Check') ";
                    } else {
                        sqlStr = "and (state = 'I' or state = 'Save' or state = 'Back') ";
                    }
                    break;
                case "reported":
                    sqlStr = "and state = 'Up' ";
                case "notChecked":
                    sqlStr = "and state = 'Submit' ";
                    break;
                case "checked":
                    sqlStr = "and state = 'Check' ";
                    break;
                default:
                    break;
            }
            map.put("stateSql", sqlStr);
        }
        result = sdsFillByDoctorMapper.selectMrlist(page,map);

        return result;
    }

    @Override
    public Map<String, Object> listByDate(Map<String, Object> params) {
        Map<String, Object> entityMap = new HashMap();
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
        List<Map<String, Object>> mrlist = sdsFillByDoctorMapper.getMrlistOfDept(params);
        
        // 单病种医生是否可以直接上报配置
        String formCheckType = CdcConfigCache.me().getValueByKey("SDS", "SDFormCheckType");
        SdsFillByDoctorDto entityDto;
        for (Map<String, Object> mrMap : mrlist) {
            Long entityId = Long.valueOf(mrMap.get("entityId").toString());
            String entityAbbre = mrMap.get("entityAbbrev").toString();
            String entityName = mrMap.get("entityName").toString();
            Integer entityStatus = (Integer) mrMap.get("entityStatus");
            if (entityMap.containsKey(entityAbbre)) {
                entityDto = (SdsFillByDoctorDto) entityMap.get(entityAbbre);
            } else {
                entityDto = new SdsFillByDoctorDto();
                entityDto.setEntityId(entityId);
                entityDto.setAbbrev(entityAbbre);
                entityDto.setBtDesc(entityName);
                entityDto.setIsActive(entityStatus);
            }
            this.calcEntityDetail(mrMap, entityDto, formCheckType);
            entityMap.put(entityAbbre, entityDto);
        }
        entityMap.put("patientNum", mrlist.size());
        return entityMap;
    }

    private void calcEntityDetail(Map<String, Object> mrMap, SdsFillByDoctorDto entityDto, String formCheckType){
        String status = mrMap.get("state").toString();
        if ("I".equals(status) || "Save".equals(status) || "Back".equals(status)) {
            //待填报
            entityDto.setNotReported(entityDto.getNotReported()+1);
        } else if ("Up".equals(status)) {
            //已上报
            entityDto.setReported(entityDto.getReported()+1);
        } else {
            if ("Check".equals(status)) {
                // 如果允许医生直接上报，则没有审核流程
                if (CommonUtil.equals(formCheckType, "Y")) {
                    //待填报
                    entityDto.setNotReported(entityDto.getNotReported()+1);
                } else {
                    // 已审核
                    entityDto.setChecked(entityDto.getChecked()+1);
                }
            } else if ("Submit".equals(status)) {
                // 如果允许医生直接上报，则没有审核流程
                if (CommonUtil.equals(formCheckType, "Y")) {
                    //待填报
                    entityDto.setNotReported(entityDto.getNotReported()+1);
                } else {
                    // 未审核
                    entityDto.setNotChecked(entityDto.getNotChecked()+1);
                }
            }
        }
        // 测试上报例数
        if (CommonUtil.equals(mrMap.get("testFlg"),1)) {
            entityDto.setTestCount(entityDto.getTestCount()+1);
        }
        // 病种入组人数
        entityDto.setPatientNum(entityDto.getPatientNum()+1);
        // 住院总天数
        if (CommonUtil.isNotEmpty(mrMap.get("admDays"))) {
            int admDays = Integer.valueOf(mrMap.get("admDays").toString());
            entityDto.setTotalDays(entityDto.getTotalDays()+admDays);
        }
        // 住院总费用
        if (CommonUtil.isNotEmpty(mrMap.get("costs"))) {
            BigDecimal costs = new BigDecimal(mrMap.get("costs").toString());
            entityDto.setTotalCost(entityDto.getTotalCost().add(costs));
        }
    }

    @Override
    public JSONObject getStatusMapByType(String type) {
        JSONObject jsObj = new JSONObject();
        jsObj.put("", "");
        List<DictionaryEntity> sds = dictionaryService.queryByTypeCode("SDS", type);
        for (DictionaryEntity dic : sds) {
            jsObj.put(dic.getCode(), dic.getDescription());
        }
        return jsObj;
    }
}